package cn.jiaqiang.chatgpt.data.domain.openai.service.rule.impl.filter;


import cn.jiaqiang.chatgpt.data.domain.openai.annotation.LogicStrategy;
import cn.jiaqiang.chatgpt.data.domain.openai.model.aggregates.ChatProcessAggregate;
import cn.jiaqiang.chatgpt.data.domain.openai.model.entity.RuleLogicEntity;
import cn.jiaqiang.chatgpt.data.domain.openai.model.entity.UserAccountEntity;
import cn.jiaqiang.chatgpt.data.domain.openai.model.valobj.LogicCheckTypeVO;
import cn.jiaqiang.chatgpt.data.domain.openai.service.rule.ILogicFilter;
import cn.jiaqiang.chatgpt.data.domain.openai.service.rule.factory.DefaultLogicFactory;
import cn.jiaqiang.chatgpt.data.types.common.RedisConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Fuzhengwei bugstack.cn @小傅哥
 * @description 用户额度扣减规则过滤
 * @create 2023-10-03 16:48
 */
@Slf4j
@Deprecated
@Component
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.USER_QUOTA)
public class UserQuotaFilter implements ILogicFilter<UserAccountEntity> {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 经过我的改进, 这里不用进行校验了, 直接返回成功即可
     */
    @Override
    public RuleLogicEntity<ChatProcessAggregate> filter(ChatProcessAggregate chatProcess, UserAccountEntity data) throws Exception {

        return RuleLogicEntity.<ChatProcessAggregate>builder()
                .type(LogicCheckTypeVO.SUCCESS)
                .data(chatProcess)
                .build();

        /* Object value = stringRedisTemplate.opsForHash().get(RedisConstants.OPENAI_WHITE_LIST_PREFIX, chatProcess.getUserId());
        if (value != null) {
            log.info("白名单用户: {} 访问, 不进行【账户额度拦截】", chatProcess.getUserId());
            return RuleLogicEntity.<ChatProcessAggregate>builder()
                    .type(LogicCheckTypeVO.SUCCESS).data(chatProcess).build();
        }

        log.info("userId = {} 开始进行【账户额度拦截】", chatProcess.getUserId());

        if (data.getSurplusQuota() > 0) {
            log.info("userId = {}, 【账户额度拦截】结果: {}", chatProcess.getUserId(), LogicCheckTypeVO.SUCCESS.getInfo());
            return RuleLogicEntity.<ChatProcessAggregate>builder()
                    .type(LogicCheckTypeVO.SUCCESS)
                    .data(chatProcess)
                    .build();
        }

        log.info("userId = {}, 【账户额度拦截】结果: {} -> 账户总额度【{}】次，已耗尽.",
                chatProcess.getUserId(), LogicCheckTypeVO.REFUSE.getInfo(), data.getTotalQuota());
        return RuleLogicEntity.<ChatProcessAggregate>builder()
                .info("个人账户，总额度【" + data.getTotalQuota() + "】次，已耗尽！")
                .type(LogicCheckTypeVO.REFUSE)
                .data(chatProcess)
                .build(); */
    }

}
